/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Controller.Materiel;
import Controller.Salle;
import Model.MaterielDAO;
import Model.SalleDAO;
import Model.TypeDAO;
import java.awt.event.ItemEvent;
import java.sql.Connection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Charly
 */
public class jframeVoirMateriel extends javax.swing.JFrame {

  private Connection connect;

  /**
   * Creates new form jFrameVoirMateriel
   */
  public jframeVoirMateriel(Connection connect) {
	initComponents();
	this.connect = connect;
	SalleDAO salleDAO = new SalleDAO(connect);
	TypeDAO typeDAO = new TypeDAO(connect);
	Map<Integer, Salle> salles = new HashMap();
	Map<String, Controller.Type> types = new HashMap();

	salles = salleDAO.findAll();
	for (Integer mapKey : salles.keySet()) {
	  // utilise ici hashMap.get(mapKey) pour accéder aux valeurs
	  cboSalle.addItem(salles.get(mapKey));
	}

	types = typeDAO.findAll();
	for (String mapKey : types.keySet()) {
	  // utilise ici hashMap.get(mapKey) pour accéder aux valeurs
	  jType.addItem(types.get(mapKey));
	}
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    cboSalle = new javax.swing.JComboBox();
    jLabel1 = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    jListMatos = new javax.swing.JTable();
    jRechercher = new javax.swing.JTextField();
    jToggleButton1 = new javax.swing.JToggleButton();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jType = new javax.swing.JComboBox();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    cboSalle.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Sans Salle" }));
    cboSalle.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(java.awt.event.ItemEvent evt) {
        cboSalleItemStateChanged(evt);
      }
    });

    jLabel1.setText("Salle");

    jListMatos.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] {

      },
      new String [] {
        "NumCB", "NumCompta", "NumSerie", "Nom", "Descriptif", "DateHT", "PrixHT", "Garantie", "NumBC", "Budget", "Amortissement", "Type", "Fournisseur", "Marque", "Secteur", "Salle"
      }
    ) {
      Class[] types = new Class [] {
        java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
      };

      public Class getColumnClass(int columnIndex) {
        return types [columnIndex];
      }
    });
    jScrollPane1.setViewportView(jListMatos);

    jToggleButton1.setText("Rechercher");
    jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jToggleButton1ActionPerformed(evt);
      }
    });

    jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
    jLabel2.setText("Recherche en fonction du CB");

    jLabel3.setText("Type");

    jType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Sans Type" }));
    jType.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    jType.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(java.awt.event.ItemEvent evt) {
        jTypeItemStateChanged(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1188, Short.MAX_VALUE)
            .addContainerGap())
          .addGroup(layout.createSequentialGroup()
            .addComponent(jRechercher, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jToggleButton1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jLabel3)
            .addGap(18, 18, 18)
            .addComponent(jType, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(36, 36, 36)
            .addComponent(jLabel1)
            .addGap(18, 18, 18)
            .addComponent(cboSalle, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(25, 25, 25))))
      .addGroup(layout.createSequentialGroup()
        .addGap(66, 66, 66)
        .addComponent(jLabel2)
        .addGap(0, 0, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jLabel2)
        .addGap(18, 18, 18)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jRechercher, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
            .addComponent(jToggleButton1)
            .addComponent(cboSalle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jLabel1)
            .addComponent(jLabel3)
            .addComponent(jType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE)
        .addContainerGap())
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void cboSalleItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboSalleItemStateChanged
	DefaultTableModel dtm = (DefaultTableModel) this.jListMatos.getModel();
	// Si on selectionne une Salle
	if (evt.getStateChange() == ItemEvent.SELECTED && (jType.getSelectedIndex() != 0 || cboSalle.getSelectedIndex() != 0)) {
	  while (dtm.getRowCount() > 0) {
		dtm.removeRow(0);
	  }

	  List<Materiel> lesMat = new LinkedList();
//	  System.out.println("type " + jType.getSelectedIndex());
//	  System.out.println("salle " + cboSalle.getSelectedIndex());
//	  System.out.println("______ ");
	  // si aucun type n'est séléctionnné mais qu'il y a une salle
	  if (jType.getSelectedIndex() == 0) {
		MaterielDAO materielDAO = new MaterielDAO(connect);
		Salle salleSelec = (Salle) cboSalle.getSelectedItem();
		lesMat = materielDAO.findBySalle(salleSelec.getIdS());
	  } // si aucune salle n'est selectionnée mais qu'un type est selectionné
	  else if (jType.getSelectedIndex() != 0 && cboSalle.getSelectedIndex() == 0) {
		MaterielDAO materielDAO = new MaterielDAO(connect);
		Controller.Type typeSelec = (Controller.Type) jType.getSelectedItem();
		lesMat = materielDAO.findByType(typeSelec.getIdT());
	  } else {
		MaterielDAO materielDAO = new MaterielDAO(connect);
		Controller.Type typeSelec = (Controller.Type) jType.getSelectedItem();
		Salle salleSelec = (Salle) cboSalle.getSelectedItem();
		lesMat = materielDAO.findByTypeEtSalle(typeSelec.getIdT(), salleSelec.getIdS());
	  }


	  for (Materiel m : lesMat) {
		Vector v = new Vector();
		v.add(m.getNumCB());
		v.add(m.getNumCompta());
		v.add(m.getNumSerie());
		v.add(m.getNom());
		v.add(m.getDescriptif());
		v.add(m.getDateHT());
		v.add(m.getPrixHT());
		v.add(m.getGarantie());
		v.add(m.getNumBC());
		v.add(m.getBudget());
		v.add(m.getAmortissement());
		v.add(m.getType().getNomT());
		v.add(m.getFournisseur().getNom());
		v.add(m.getMarque().getNomM());
		v.add(m.getSecteur().getNom());
		v.add(m.getSalle().getNomS());
		dtm.addRow(v);
	  }
	} else {
	  while (dtm.getRowCount() > 0) {
		dtm.removeRow(0);
	  }
	}
  }//GEN-LAST:event_cboSalleItemStateChanged
  private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
	DefaultTableModel dtm = (DefaultTableModel) this.jListMatos.getModel();
	while (dtm.getRowCount() > 0) {
	  dtm.removeRow(0);
	}

	Long CB = Long.valueOf(this.jRechercher.getText());
	List<Materiel> lesMat = new LinkedList();
	MaterielDAO materielDAO = new MaterielDAO(connect);

	lesMat = materielDAO.findByCB(CB);

	for (Materiel m : lesMat) {
	  Vector v = new Vector();
	  v.add(m.getNumCB());
	  v.add(m.getNumCompta());
	  v.add(m.getNumSerie());
	  v.add(m.getNom());
	  v.add(m.getDescriptif());
	  v.add(m.getDateHT());
	  v.add(m.getPrixHT());
	  v.add(m.getGarantie());
	  v.add(m.getNumBC());
	  v.add(m.getBudget());
	  v.add(m.getAmortissement());
	  v.add(m.getType().getNomT());
	  v.add(m.getFournisseur().getNom());
	  v.add(m.getMarque().getNomM());
	  v.add(m.getSecteur().getNom());
	  v.add(m.getSalle().getNomS());
	  dtm.addRow(v);
	}
  }//GEN-LAST:event_jToggleButton1ActionPerformed

  private void jTypeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jTypeItemStateChanged
	DefaultTableModel dtm = (DefaultTableModel) this.jListMatos.getModel();
	// Si on selectionne un Type
	if (evt.getStateChange() == ItemEvent.SELECTED && (jType.getSelectedIndex() != 0 || cboSalle.getSelectedIndex() != 0)) {
	  //vide le tableau
	  while (dtm.getRowCount() > 0) {
		dtm.removeRow(0);
	  }

	  List<Materiel> lesMat = new LinkedList();
//	  System.out.println("type " + jType.getSelectedIndex());
//	  System.out.println("salle " + cboSalle.getSelectedIndex());
//	  System.out.println("______ ");
	  // si aucune salle n'est séléctionnnée mais il y a un type
	  if (cboSalle.getSelectedIndex() == 0) {
		MaterielDAO materielDAO = new MaterielDAO(connect);
		Controller.Type typeSelec = (Controller.Type) jType.getSelectedItem();
		lesMat = materielDAO.findByType(typeSelec.getIdT());
	  } // si aucune type n'est selectionné mais qu'une salle est selectionnée
	  else if (cboSalle.getSelectedIndex() != 0 && jType.getSelectedIndex() == 0) {
		MaterielDAO materielDAO = new MaterielDAO(connect);
		Salle salleSelec = (Salle) cboSalle.getSelectedItem();
		lesMat = materielDAO.findBySalle(salleSelec.getIdS());
	  } // Si pas de type ni de salle
	  else {
		MaterielDAO materielDAO = new MaterielDAO(connect);
		Controller.Type typeSelec = (Controller.Type) jType.getSelectedItem();
		Salle salleSelec = (Salle) cboSalle.getSelectedItem();
		lesMat = materielDAO.findByTypeEtSalle(typeSelec.getIdT(), salleSelec.getIdS());
	  }

	  for (Materiel m : lesMat) {
		Vector v = new Vector();
		v.add(m.getNumCB());
		v.add(m.getNumCompta());
		v.add(m.getNumSerie());
		v.add(m.getNom());
		v.add(m.getDescriptif());
		v.add(m.getDateHT());
		v.add(m.getPrixHT());
		v.add(m.getGarantie());
		v.add(m.getNumBC());
		v.add(m.getBudget());
		v.add(m.getAmortissement());
		v.add(m.getType().getNomT());
		v.add(m.getFournisseur().getNom());
		v.add(m.getMarque().getNomM());
		v.add(m.getSecteur().getNom());
		v.add(m.getSalle().getNomS());
		dtm.addRow(v);
	  }
	} else {
	  while (dtm.getRowCount() > 0) {
		dtm.removeRow(0);
	  }
	}
  }//GEN-LAST:event_jTypeItemStateChanged
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JComboBox cboSalle;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JTable jListMatos;
  private javax.swing.JTextField jRechercher;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JToggleButton jToggleButton1;
  private javax.swing.JComboBox jType;
  // End of variables declaration//GEN-END:variables
}
